package com.wei.duowan.bbs.ui.activity;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.WindowManager;

import com.gyf.immersionbar.ImmersionBar;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.wei.duowan.bbs.presenter.BasePresenter;
import com.wei.duowan.bbs.utils.CacheFileUtils;
import com.wei.duowan.bbs.utils.RxUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;

/**
 * 作者：赵若位
 * 时间：2019/7/31 23:28
 * 联系方式：1070138445
 * 功能：
 */
public class SplashActivity extends BaseActivity
{

    @Override
    protected BasePresenter createPresenter()
    {
        return null;
    }

    @Override
    protected int getLayoutID()
    {
        return 0;
    }

    @Override
    protected void initView()
    {
        /*延迟启动MainActivity*/
        RxPermissions permissions = new RxPermissions(this);
        permissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(aBoolean ->
                {
                    CacheFileUtils.getInstance().setIsPermission(aBoolean);
                    delay(1500);
                });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setStatusColor()
    {
        ImmersionBar.with(this).transparentBar().fullScreen(true).init();
    }

    /*延迟启动Activity*/
    private void delay(@Nullable long time)
    {
        Observable.empty()
                .delay(time, TimeUnit.MILLISECONDS)
                .compose(RxUtils.getSchedulers())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .doFinally(() ->
                {
                    openActivity(MainActivity.class);
                    doFinish();
                }).subscribe();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        getWindow().setBackgroundDrawable(null);
    }
}
